/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Kernel Module                                  */
/*                                                                           */
/*    Copyright (C) 2005-2013 Laszlo Arvai                                   */
/*                                                                           */
/*    ------------------------------------------------------------------   */
/*    Socket utility functions                                               */
/*****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Includes
#include <netSocket.h>
#include <netSocketUtils.h>

///////////////////////////////////////////////////////////////////////////////
//! Converts IP address to string
//! \param Buffer to store conversion result
//! \param Length of the buffer
//! \param Start position of the stored string
//! \param IP address to convert
dosStringLength netIPAddressToStringPos( dosString in_buffer, dosStringLength in_buffer_length, dosStringLength in_pos, IN_ADDR* in_address )
{
	dosByte i;
	for(i = 0; i < 4; i++)
	{
		if(i > 0)
			in_pos = strCopyCharacter(in_buffer, in_buffer_length, in_pos, '.');

		in_pos = strWordToStringPos(in_buffer, in_buffer_length, in_pos, in_address->v[i], 0, 0, 0);
	}
	in_buffer[in_pos] = '\0';

	return in_pos;
}

///////////////////////////////////////////////////////////////////////////////
//! Converts String to IP address
//! \param Buffer to parse for IP address
//! \param Length of the buffer
//! \param Start position for the parser
//! \param Success flag
//! \param IP address storage
void netStringToIPAddress(dosString in_buffer, dosStringLength in_buffer_length, dosStringLength* in_pos, dosBool* in_success, IN_ADDR* out_address)
{
	dosByte byte1, byte2, byte3, byte4;

	strStringToByte( in_buffer, in_buffer_length, in_pos, in_success, &byte1 );
  strCheckForSeparator( in_buffer, in_buffer_length, in_pos, in_success, '.' );
  strStringToByte( in_buffer, in_buffer_length, in_pos, in_success, &byte2 );
  strCheckForSeparator( in_buffer, in_buffer_length, in_pos, in_success, '.' );
  strStringToByte( in_buffer, in_buffer_length, in_pos, in_success, &byte3 );
  strCheckForSeparator( in_buffer, in_buffer_length, in_pos, in_success, '.' );
  strStringToByte( in_buffer, in_buffer_length, in_pos, in_success, &byte4 );

	if(*in_success)
  {
		out_address->v[0] = byte1;
		out_address->v[1] = byte2;
		out_address->v[2] = byte3;
		out_address->v[3] = byte4;
	}
}
